﻿using CommonUtility;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Model.PageModel;
using Models.DTO.Api;
using Models.DTO.Api.page.Form;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Security.Policy;
using WPay.Domain.MainModule.Interface.DomainServices;
using WPay.Domain.MainModule.Interface.RepositoryServices;
using WPay.Domain.Model.DTOs;
using WPay.Domain.Model.DTOs.Parms;
using WPay.Domain.Model.Entities;
using WPay.Domain.Model.Sky;

namespace WPay.Web.API.Controllers
{
    /// <summary>
    /// 系统管理
    /// </summary>
    public class SysController : BaseController
    {
        private readonly IMngrBaseInfo_RepositoryService _mngrBaseInfo_RepositoryService;
        private readonly IMngrBaseInfo_DomainService _mngrBaseInfo_DomainService;
        private readonly ISysRole_RepositoryService _sysRole_RepositoryService;
        private readonly ISysRole_DomainService _sysRole_DomainService;
        private readonly ISysModule_DomainService _sysModule_DomainService;
        private readonly ISysModule_RepositoryService _sysModule_RepositoryService;
   
        private readonly IMngrLog_RepositoryService _mngrlog_RepositoryService;
 
     
  

        public SysController(IMngrBaseInfo_RepositoryService mngrBaseInfo_RepositoryService,
            ISysRole_RepositoryService sysRole_RepositoryService, ISysRole_DomainService sysRole_DomainService,
            IMngrBaseInfo_DomainService mngrBaseInfo_DomainService, ISysModule_DomainService sysModule_DomainService
            , ISysModule_RepositoryService sysModule_RepositoryService
          ,IMngrLog_RepositoryService mngrlog_RepositoryService
            )
        {
            _mngrBaseInfo_RepositoryService = mngrBaseInfo_RepositoryService;
            _sysRole_RepositoryService = sysRole_RepositoryService;
            _sysRole_DomainService = sysRole_DomainService;
            _mngrBaseInfo_DomainService = mngrBaseInfo_DomainService;
            _sysModule_DomainService = sysModule_DomainService;
            _sysModule_RepositoryService = sysModule_RepositoryService;
           
            _mngrlog_RepositoryService = mngrlog_RepositoryService;
         
           
           
      
           
        }
        #region Public
        /// <summary>
        /// 获取模块下拉
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public RespDto GetFatherModuleSelect()
        {
            return new RespDto().RunWithTry(x =>
            {
                x.Data = _sysModule_RepositoryService.GetFatherModule(HttpContext.Request.Query["fatherId"], HttpContext.Request.Query["itemId"]);

            });
        }
        #endregion

        #region 系统用户管理
        /// <summary>
        /// 获取系统用户列表
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public RespDto GetManagerList()
        {
            return Do<MangerUserParms>((x, result) =>
            {
                var Data = _mngrBaseInfo_RepositoryService.GetManagerList(x.PageParm.page, x.PageParm.rows, Function.InitSession(HttpContext), x);
                result.Data = GetRowToGrid(Data);
            });
        }
        /// <summary>
        /// 获取系统用户列表
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public RespDto GetManagerAll()
        {
            return Do<MangerUserParms>((x, result) =>
            {
                var Data = _mngrBaseInfo_RepositoryService.GetManagerAll(Function.InitSession(HttpContext), x);
                result.Data = Data;
            });
        }
        /// <summary>
        /// 加载当前企业下角色
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public RespDto LoadMangerRoleList()
        {
            return new RespDto().RunWithTry(x =>
            {
                x.Data = _sysRole_RepositoryService.LoadMangerRoleList(HttpContext.Request.Query["id"], Function.InitSession(HttpContext));
            });
        }
        /// <summary>
        /// 添加用户
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public RespDto AddMangerBaseInfo()
        {
            return Do<MngrBaseInfo>((x, result) =>
            {
                //x.CpnId = Function.InitSession(HttpContext).CpnId;
                var Data = _mngrBaseInfo_DomainService.AddMangerBaseInfo(x, HttpContext.Request);

            });
        }
        /// <summary>
        /// 删除用户
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public RespDto DelManager()
        {
            return new RespDto().RunWithTry(x =>
            {
                _mngrBaseInfo_DomainService.DelManager(HttpContext.Request, Function.InitSession(HttpContext).ID);
            });
        }
        #endregion

        #region 角色权限管理
        /// <summary>
        /// 获取角色列表
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public RespDto GetRoleList()
        {
            return Do<RoleParms>((x, result) =>
            {
                var Data = _sysRole_RepositoryService.GetRoleList(x.PageParm.page, x.PageParm.rows, Function.InitSession(HttpContext), x);
                result.Data = GetRowToGrid(Data);
            });
        }
        /// <summary>
        /// 加载模块信息
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public RespDto LoadModuleList()
        {
            return new RespDto().RunWithTry(x =>
            {
                x.Data = _sysRole_RepositoryService.LoadModuleList(HttpContext.Request.Query["roleid"], Function.InitSession(HttpContext));
            });
        }

        /// <summary>
        /// 添加角色
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public RespDto SaveRole()
        {
            return Do<SysRole>((x, result) =>
            {
                _sysRole_DomainService.AddRole(HttpContext.Request, x, Function.InitSession(HttpContext));
            });
        }
        /// <summary>
        /// 删除角色
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public RespDto DelRole()
        {
            return new RespDto().RunWithTry(x =>
            {
                _sysRole_DomainService.DelRole(HttpContext.Request);
            });
        }
        /// <summary>
        /// 获取所有收款账户
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public RespDto GetAllAct()
        {
            return new RespDto().RunWithTry(x =>
            {
                x.Data = _sysRole_RepositoryService.GetAllAct(Function.InitSession(HttpContext));
            });
        }
        #endregion

        #region 模块管理

        /// <summary>
        /// 获取模块数据
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public RespDto GetModuleLst()
        {
            return Do<ModuleParmsDTO>((x, result) =>
            {
                //AppFunction.FuctionLogin(HttpContext);
                result.Data = GetRowToGrid(_sysModule_RepositoryService.GetModuleList(x));

            });
        }
        /// <summary>
        /// 获当前模块下子模块
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public RespDto GetModuleListByPid()
        {
            return Do<ModuleParmsDTO>((x, result) =>
            {
                result.Data = GetRowToGrid(_sysModule_RepositoryService.GetModuleListByPid(x));

            });
        }
        /// <summary>
        /// 获取模块数据
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public RespDto GetSysModuleActionLst()
        {
            return Do<SysModuleActionParms>((x, result) =>
            {
                result.Data = GetRowToGrid(_sysModule_RepositoryService.GetSysModuleActionLst(x.PageParm.page, x.PageParm.rows, x));

            });
        }
        /// <summary>
        /// 获取菜单详情
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public RespDto GetSysModuleActionById()
        {
            return Do<EmptyDTO>((x, result) =>
            {
                result.Data = _sysModule_RepositoryService.GetSysModuleActionById(HttpContext.Request.Form["Id"]);

            });
        }
        /// <summary>
        /// 获取菜单详情
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public RespDto GetmoduleById()
        {
            return Do<EmptyDTO>((x, result) =>
            {
                result.Data = _sysModule_RepositoryService.GetmoduleById(HttpContext.Request.Form["Id"]);

            });
        }
        /// <summary>
        /// 添加/修改模块
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public RespDto AddSysModule()
        {
            return Do<SysModuleFormDTO>((x, result) =>
            {
                _sysModule_DomainService.AddSysModule(x);
                result.Success = true;

            });
        }
        /// <summary>
        /// 删除模块
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public RespDto DeleteSysModule()
        {
            return Do<EmptyDTO>((x, result) =>
            {
                result.Success = _sysModule_DomainService.DeleteSysModule(HttpContext.Request.Form["Id"]);

            });
        }
        /// <summary>
        /// 添加菜单
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public RespDto AddSysModuleAction()
        {
            return Do<SysModuleActionFormDTO>((x, result) =>
            {

                _sysModule_DomainService.AddSysModuleAction(x);
                result.Success = true;

            });
        }
        /// <summary>
        /// 添加菜单
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public RespDto DeleteSysModuleAction(string ids)
        {
            return Do<EmptyDTO>((x, result) =>
            {
                _sysModule_DomainService.DeleteSysModuleAction(HttpContext.Request.Form["ids"]);
                result.Success = true;

            });
        }
        #endregion

    

        #region 系统日志管理
        /// <summary>
        /// 获取MngrLog
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public RespDto GetMngrLogLst()
        {
            return Do<MngrLogParms>((x, result) =>
            {
                var data = _mngrlog_RepositoryService.GetMngrLogLst(x.PageParm.page, x.PageParm.rows, x);
                result.Data = GetRowToGrid(data);
            });
        }
        #endregion

      




        /// <summary>
        /// 接受报警
        /// </summary>
        /// <param name="List"></param>
        /// <param name=""></param>
        /// <param name=""></param>
        [HttpPost]
        [HttpGet]
        [AllowAnonymous]
        public void AlarmMsg([FromBody] List<SkyError> msgs)
        {
            try
            {   
                LoggerHelper.Writelog("AlarmMsg", Newtonsoft.Json.JsonConvert.SerializeObject(msgs));
               
                
            }
            catch (Exception e)
            {
                LoggerHelper.Writelog("AlarmMsg", e.Message + e.InnerException?.Message);
            }

        }
    }
}